﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// ad_list 的摘要说明
    /// </summary>
    public class ad_list : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            int category_id = DTRequest.GetQueryInt("category_id");
            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
            BLL.advert bll = new BLL.advert();
            DataSet ds = bll.GetList(0, "category_id=" + category_id, "sort_id asc,id asc");
            //如果记录存在
            if (ds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"slide_list\":[");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"company_id\":" + dr["company_id"]);
                    string company_name = string.Empty;
                    if (int.Parse(dr["company_id"].ToString()) > 0)
                    {
                        company_name = new BLL.company().GetTitle(int.Parse(dr["company_id"].ToString()));
                        if (company_name.Length > 6)
                        {
                            company_name = company_name.Substring(0, 6) + "…";
                        }
                    }
                    strTxt.Append(",\"company_name\":\"" + company_name + "\"");
                    string _imgurl = dr["img_url"].ToString();
                    if (string.IsNullOrEmpty(_imgurl))
                    {
                        strTxt.Append(",\"img_url\":\"../image/error-img.png\"");
                    }
                    else if (_imgurl.Contains("shicaiquanzi"))
                    {
                        strTxt.Append(",\"img_url\":\"http://" + dr["img_url"] + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"img_url\":\"http://pic.shicaiquanzi.com" + dr["img_url"] + "\"");
                    }
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("]");
            }
            strTxt.Append("}");
            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}